/**
 * 
 */
package com.efast.dmom.mes.datahandlers.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.Lot;
import com.efast.dmom.mes.bean.Route;
import com.efast.dmom.mes.bean.Unit;
import com.efast.dmom.mes.bean.WorkOrderItem;
import com.efast.dmom.mes.dao.entity.LotDao;
import com.efast.dmom.mes.entity.LotEntity;
import com.efast.dmom.mes.entity.RouteEntity;
import com.efast.dmom.mes.entity.UnitEntity;
import com.efast.dmom.mes.entity.WorkOrderItemEntity;
import com.efast.dmom.mes.entity.base.ITraceObject;

/**
 * @author Aaron Liu
 *
 *         2020年3月30日
 */
@Component
public class LotDataHandler {

	@Autowired
	private RouteDataHandler routeDataHandler = null;
	
	@Autowired
	private LotDao lotDao = null;

	@Autowired
	private UnitDataHandler unitDataHandler = null;
	
	
	public Lot createLot(WorkOrderItem orderItem, long routeKey, short lotType) {

		if (orderItem == null || orderItem.getInnerData() == null) {
			return null;
		}

		Lot lot = new Lot();

		LotEntity lotEntity = new LotEntity();

		Route route = routeDataHandler.getRouteByKey(routeKey);
		// entity.setOriginalLotKey(originalLotKey);
		WorkOrderItemEntity orderItemEntity = orderItem.getInnerData();
		BigDecimal quantityOrdered = orderItemEntity.getQuantityOrdered();
		
		BigDecimal quantityInprogress = (orderItemEntity.getQuantityInProgress() != null)
				? orderItemEntity.getQuantityInProgress().add(quantityOrdered) : quantityOrdered;

		if (quantityOrdered != null && quantityOrdered.intValue() > 0) {
			orderItemEntity.setQuantityInProgress(quantityInprogress);
		}

		lotEntity.setOrderItemKey(orderItemEntity.getId());
		lotEntity.setOrderKey(orderItemEntity.getOrderKey());
		lotEntity.setBomName(orderItemEntity.getBomName());
		lotEntity.setBomRevision(orderItemEntity.getBomRevison());
		lotEntity.setPartNumber(orderItemEntity.getPartNumber());
		lotEntity.setPartRevision(orderItemEntity.getPartRevision());
		lotEntity.setLotType(lotType);
		lotEntity.setQuantity(quantityInprogress);
		lotEntity.setRouteKey(orderItemEntity.getRouteId());

		if (route != null) {

			RouteEntity routeEntity = route.getInnerData();
			if (routeEntity != null) {
				lotEntity.setProductionLineKey(routeEntity.getDefaultProdlineKey());
				lotEntity.setRouteKey(routeEntity.getId());
			}
		}
		lotDao.save(lotEntity);
		lot.setInnerData(lotEntity);

		if ((orderItemEntity.getTraceType() == ITraceObject.LOT_TRACE_TYPE_UNIT)
				&& (ITraceObject.LOT_TYPE_NORMAL == lotType || ITraceObject.LOT_TYPE_SERIALIZED == lotType)) {

			List<Unit> unitList = new ArrayList<Unit>();

			if (quantityOrdered != null && quantityOrdered.intValue() > 0) {

				for (int i = 0; i < quantityOrdered.intValue(); i++) {

					Unit unit = new Unit();
					
					UnitEntity unitEntity = new UnitEntity();
					unitEntity.setLotKey(lot.getInnerData().getId());
					unitEntity.setOrderKey(orderItemEntity.getOrderKey());
					unitEntity.setOrderItemKey(orderItemEntity.getId());
					unitEntity.setPartNumber(orderItemEntity.getPartNumber());
					unitEntity.setPartRevision(orderItemEntity.getPartRevision());
					unitEntity.setRouteKey(lotEntity.getRouteKey());
					// unitEntity.setSerialNumber(serialNumber);
					
					unit.setInnerData(unitEntity);
					unitList.add(unit);
				}
			}
			
			if(unitList.size() > 0){
				unitDataHandler.saveUnit(unitList);
			}
		}
		return lot;
	}

	public void createLot(WorkOrderItem orderItem, int batch, BigDecimal batchSize) {

		if (orderItem == null || batchSize == null || batchSize.intValue() < 0) {
			return;
		}

		WorkOrderItemEntity itemEntity = orderItem.getInnerData();

		if (itemEntity.getTraceType() != ITraceObject.LOT_TRACE_TYPE_BATCH) {
			return;
		}

		BigDecimal orderQuantity = itemEntity.getQuantityOrdered();
		if (orderQuantity == null || orderQuantity.intValue() < batch * batchSize.intValue()) {
			return;
		}

		List<LotEntity> entityList = new ArrayList<LotEntity>();

		for (int i = 0; i < batch; i++) {

			LotEntity lotEntity = new LotEntity();

			lotEntity.setBomName(itemEntity.getBomName());
			lotEntity.setBomRevision(itemEntity.getBomRevison());
			// lotEntity.setLotName(lotName);
			lotEntity.setLotType(ITraceObject.LOT_STATUS_CREATED);

			BigDecimal counter = new BigDecimal(i + 1);
			BigDecimal left = orderQuantity.subtract(counter.multiply(batchSize));
			if (!(left.compareTo(BigDecimal.ZERO) == -1)) {
				lotEntity.setQuantity(batchSize);
			} else {
				lotEntity.setQuantity(left);
			}

			lotEntity.setOrderKey(itemEntity.getOrderKey());
			lotEntity.setPartNumber(itemEntity.getPartNumber());
			lotEntity.setPartRevision(itemEntity.getPartRevision());
			lotEntity.setLotType(ITraceObject.LOT_TYPE_NORMAL);

			entityList.add(lotEntity);
		}
		
		lotDao.saveLotList(entityList);
	}
	
	public void save(LotEntity target){;
	
	}
	
	public void startAtWorkCenter(long lotKey, String routeStepName, String workCenterName, String comments) {

	}

	public Lot createStandAloneLot(String lotName, BigDecimal quantity, String partNumber, String partRevision,
			String bomName, String bomRevision, long routeKey, short lotType) {

		Lot lot = new Lot();

		Route route = routeDataHandler.getRouteByKey(routeKey);

		LotEntity entity = new LotEntity();

		entity.setStandAlone(true);
		entity.setLotType(lotType);
		entity.setLotName(lotName);
		entity.setQuantity(quantity);

		if (route != null) {
			entity.setRouteKey(route.getInnerData().getId());
		}
		// TODO
		entity.setPriority(0);
		entity.setBomName(bomName);
		entity.setBomRevision(bomRevision);
		entity.setPartNumber(partNumber);
		entity.setPartRevision(partRevision);

		lot.setInnerData(entity);

		if (ITraceObject.LOT_TYPE_UNSERIALIZED == lotType) {

		}

		if (ITraceObject.LOT_TYPE_NORMAL == lotType || ITraceObject.LOT_TYPE_BULK == lotType) {

			// Create units
		}

		return lot;
	}

}
